package com.zms.maze;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Outputer {
	private ByteArrayOutputStream output=new ByteArrayOutputStream();
	private PrintWriter writer=new PrintWriter(output);
	private String parameter;
	private InvocationHandler handler=new InvocationHandler() {	
		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
			String name=method.getName();
			if("getWriter".equals(name)) {
				return writer;
			} else if("getParameter".equals(name)) {
				return parameter;
			}
			return null;
		}	
	};
	private HttpServletRequest request=(HttpServletRequest)Proxy.newProxyInstance(this.getClass().getClassLoader(),new Class[]{HttpServletRequest.class},handler);
	private HttpServletResponse response=(HttpServletResponse)Proxy.newProxyInstance(this.getClass().getClassLoader(),new Class[]{HttpServletResponse.class},handler);
	{
		String name=this.getClass().getName();
		name=name.replace('.','/')+".class";
		URL url=this.getClass().getClassLoader().getResource(name);
		name=url.getFile();
		name=name.substring(0,name.indexOf("/WEB-INF/"));
		MazeContextListener.parentPath=name;		
	}
	private ShowMaze show=new ShowMaze();
	public String getContent(int n) throws Exception {
		parameter=String.valueOf(n);
		writer.flush();
		output.reset();
		show.doGet(request,response);
		writer.flush();
		return new String(output.toByteArray());
	}
	public int getCount() {
		return Maze.getCount();
	}
	private String url ="jdbc:mysql://mysql-m/maze?user=maze&password=";
	private String options="&useUnicode=true&characterEncoding=8859_1";
	private int start=1;
	public void doit() throws Exception {
		Class.forName("org.gjt.mm.mysql.Driver");
		Connection conn=null;
		PreparedStatement statement=null;
		try {
			conn=DriverManager.getConnection(url);
			conn.setAutoCommit(false);
			int total=getCount();			
			for(int i=start;i<=total;i++) {
				statement=conn.prepareStatement("delete from mazes where num=?");
				statement.setInt(1,i);
				statement.executeUpdate();
				conn.commit();
				statement.close();
				statement=conn.prepareStatement("insert into mazes values (?,?)");
				statement.setInt(1,i);
				statement.setString(2,getContent(i));
				if(statement.executeUpdate()==1) {
					System.out.println("insert ok "+i);
				}
				conn.commit();
				statement.close();
				statement=null;
			}			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(statement!=null) {
					statement.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}		
	}
	 
	public static void main(String[] args) throws Exception {
		Outputer outputer=new Outputer();
		BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
		String line=reader.readLine();
		outputer.url=outputer.url+line+outputer.options;
		line=reader.readLine();
		outputer.start=Integer.parseInt(line);
		outputer.doit();
	}
}